package com.tianchi.lexiang.background;

import java.io.File;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StatFs;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.bean.LeXiangConstants;

public class HardDiskSizeInfo extends Activity {

	private TextView totalCapactiyTxt;
	private TextView residualCapactiyTxt;
	private TextView hardDiskTotalCapactiyTxt;
	private TextView hardDiskResidualCapactiyTxt;
	
	private Button sureBtn;
	
	private ProgressDialog progressDialog;
	
	long totalSize = 0;
	long residualSize = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hard_disk_size_info);

		hardDiskTotalCapactiyTxt = (TextView) findViewById(R.id.hard_disk_total_capactiy_txt);
		totalCapactiyTxt = (TextView) findViewById(R.id.total_capactiy_id);
		hardDiskResidualCapactiyTxt = (TextView) findViewById(R.id.hard_disk_residual_capactiy_txt);
		residualCapactiyTxt = (TextView) findViewById(R.id.residual_capactiy_id);

		sureBtn = (Button) findViewById(R.id.sure_btn);
		sureBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				HardDiskSizeInfo.this.finish();
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();
		progressDialog = ProgressDialog.show(HardDiskSizeInfo.this, getString(R.string.harddisk_size_info),
				getString(R.string.are_harddisk_size_info), true); 
		new HardDiskSizeTask().execute();
	}
	
	class HardDiskSizeTask extends AsyncTask<String, String, Boolean>{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Boolean doInBackground(String... params) {
			File file = new File(LeXiangConstants.SONG_HARD_DISK_PATH);
			if(file.exists()){
				getHardDiakSize();
				return true;
			}else{
				return false;
			}
			
			
		}
		
		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if(result){
				totalCapactiyTxt.setText(totalSize + getString(R.string.gb));
				residualCapactiyTxt.setText(residualSize + getString(R.string.gb));
			}else{
				hardDiskTotalCapactiyTxt.setVisibility(View.GONE);
				totalCapactiyTxt.setText(R.string.check_hdd_tip);
				hardDiskResidualCapactiyTxt.setVisibility(View.GONE);
				residualCapactiyTxt.setText("");
			}
			progressDialog.dismiss();
		}
	}
	
	private void getHardDiakSize(){
			StatFs sf = new StatFs(LeXiangConstants.SONG_HARD_DISK_PATH);
			long blockSize = sf.getBlockSize();
			long blockCount = sf.getBlockCount();
			long availCount = sf.getAvailableBlocks();
			totalSize = blockSize * blockCount / 1024 / 1024 / 1024;
			residualSize = availCount * blockSize / 1024 / 1024 / 1024;
	}
	

}
